package com.savitar.security.controller;

import com.savitar.account.entity.Account;
import com.savitar.account.service.AccountService;
import com.savitar.common.enums.HttpStatus;
import com.savitar.common.vo.ResultBean;
import com.savitar.security.entity.LoginVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.security.Principal;

@RequestMapping
@RestController
public class LoginController {

    @Autowired
    private AccountService accountService;

    @PostMapping("/login")
    public ResultBean login(@RequestBody LoginVo loginVo) {
        return accountService.login(loginVo.getUsername(), loginVo.getPassword());
    }

    @PostMapping("/me")
    public ResultBean me(Principal principal) {
        if (principal == null) {
            return ResultBean.err(HttpStatus.NOT_LOGIN.getCode(), HttpStatus.NOT_LOGIN.getDesc());
        }
        Account account = accountService.loadUserByUsername(principal.getName());
        account.setPassword(null);
        return ResultBean.ok(account);
    }

    @PostMapping("/logout")
    public ResultBean logout() {
        return ResultBean.ok(null);
    }
}
